﻿@page "/logger/custom"
@inject ILogCustomException ilogCustomException
<div class="site-button-ghost-wrapper">
    @*<Input Placeholder="Search Document" @bind-Value="@txtValue" Bordered="false" ClassicSearchIcon />*@
    @*<Search Placeholder="Search Document" OnPressEnter="() => searchAction()" WrapperStyle="width: 200px;" @bind-Value="@searchValue" Bordered="false" ClassicSearchIcon />*@

    <Input Placeholder="Search" style="width:150px;" @bind-Value="@txtValue" />
    <RangePicker TValue="DateTime?[]" ShowTime='@("HH:mm")' OnChange="OnRangeChange" />
    <Button Type="primary" Icon="search" Loading="noIconLoading" OnClick="EnterWithIconLoading">
        Search
    </Button>
</div>
<Table @ref="table"
       TItem="LogCustomException"
       DataSource="@forecasts"
       Total="pagination.records"
       @bind-PageIndex="pagination.page"
       @bind-PageSize="pagination.rows"
       @bind-SelectedRows="selectedRows" ScrollX="1200" ScrollY="500" Loading="_loading" OnChange="HandleTableChange">
    <RowTemplate>
        <Column @bind-Field="@context.Level" Width="100"> <Tag Color="green">@context.Level</Tag> </Column>
        <Column @bind-Field="@context.Message" Width="200" Ellipsis />
        <Column @bind-Field="@context.CreateTime" Format="yyyy-MM-dd HH:mm:ss" Width="200" />
        <Column @bind-Field="@context.Category" Width="150" Ellipsis />
        <Column @bind-Field="@context.Exception" Ellipsis />
    </RowTemplate>

    <ExpandTemplate>
        <Timeline>
            <TimelineItem>@context.Data.CreateTime</TimelineItem>
            <TimelineItem Color="red">[@context.Data.Level] @context.Data.Category</TimelineItem>
            <TimelineItem>@context.Data.Exception</TimelineItem>
        </Timeline>
    </ExpandTemplate>
</Table>


<style>
    .ant-form-item-label {
        flex: 0 0 130px !important;
    }

    .ant-col-offset-8 {
        margin-left: 130px;
    }

    .site-button-ghost-wrapper {
        background: rgb(190, 200, 200);
        padding: 26px 16px 16px;
    }

    .add-icon {
        border: 1px solid #abaaaa;
        color: #797777;
        border-radius: 50%;
        width: 24px;
        height: 24px;
        text-align: center;
        line-height: 24px;
        margin-top: 4px;
        margin-left: 20px;
        padding-right: 1px;
        cursor: pointer;
    }
</style>


@using System.Text.Json;
@code {
    bool noIconLoading = false;
    string txtValue { get; set; }
    DateTime? startdate { get; set; }
    DateTime? enddate { get; set; }
    WindowsHelper windows;
    bool _loading = false;
    string _selectedValue;
    string searchValue { get; set; }
    private string _sizeValue = AntSizeLDSType.Default;
    List<LogCustomException> forecasts = new List<LogCustomException>();
    IEnumerable<LogCustomException> selectedRows;
    ITable table;
    Core.DataAccess.Model.Pagination pagination = new Core.DataAccess.Model.Pagination
    {
        page = 1,
        rows = 7
    };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            windows = new WindowsHelper(jSRuntime);
            forecasts = await GetForecastAsync();
            await windows.HideLoadingAsync();
            await windows.SetTitleAsync("Custom Logger");
            _ = InvokeAsync(StateHasChanged);
        }
    }
    private async Task HandleTableChange(QueryModel<LogCustomException> queryModel)
    {
        if (windows != null)
        {
            this.pagination.page = queryModel.PageIndex;
            this.pagination.rows = queryModel.PageSize;
            await this.LoadData();
        }
    }
    private async Task LoadData()
    {
        this._loading = true;
        await base.InvokeAsync(async () =>
        {
            forecasts = await GetForecastAsync();
            this._loading = false;
            base.StateHasChanged();
        });
    }
    async Task searchAction()
    {
        forecasts = await GetForecastAsync();
        this.StateHasChanged();
        //Console.WriteLine($"Collapsed: {this.searchValue}");
        // 搜索方法
        // await Task.Delay(100);
    }
    private async Task EnterWithIconLoading()
    {
        noIconLoading = true;
        forecasts = await GetForecastAsync();
        this.StateHasChanged();
        noIconLoading = false;
    }
    private void OnRangeChange(DateRangeChangedEventArgs args)
    {
        startdate = args.Dates[0];
        enddate = args.Dates[1];
        //Console.WriteLine($"Selected Time: {JsonSerializer.Serialize(args.Dates)}");
        //Console.WriteLine($"Formatted Selected Time: {JsonSerializer.Serialize(args.DateStrings)}");
    }
    public async Task<List<LogCustomException>> GetForecastAsync()
    {
        if (string.IsNullOrWhiteSpace(AppSetting.iToolLoggerSqlString)) return new List<LogCustomException>();

        var result = await ilogCustomException.GetCustom(pagination, this.txtValue, this.startdate, this.enddate);

        return result;

    }

}
